package com.shop.controller;

import com.shop.annotation.RequiresPermissionsDesc;
import com.shop.pojo.DtsUser;
import com.shop.pojo.DtsUserAccount;
import com.shop.service.DtsUserAccountService;
import com.shop.service.DtsUserService;
import com.shop.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName AdminUserController
 * @Author xiao宋
 * @Date 14:11  2022/10/15
 * @Version 1.0
 * @Description TODO
 */
@Slf4j
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {
    @Autowired
    DtsUserService userService;
    @Autowired
    DtsUserAccountService accountService;

    /**
     * 显示用户列表
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param username 通过用户名搜索
     * @param mobile   通过手机号搜索
     * @return
     */
    @RequiresPermissions("admin:user:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "用户管理"}, button = "用户查询")
    @GetMapping("/list")
    public Object UserList(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
            @RequestParam(value = "sort", required = false, defaultValue = "add_time") String sort,
            @RequestParam(value = "order", required = false, defaultValue = "desc") String order,
            String username, String mobile
    ) {
        log.debug("显示用户列表分页方法");
        log.debug("前端传过来的每页条数：{}，第几页：{}", page, limit);
        Map<String, Object> data = userService.findByPage(page, limit, sort, order, username, mobile);
        log.debug("用户列表分页方法显示完毕");
        return ResponseUtil.ok(data);
    }

    /**
     * 设置佣金比例
     *
     * @return 是否更新数据成功
     */
    @RequiresPermissions("admin:user:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "用户管理"}, button = "用户修改")
    @PostMapping("/approveAgency")
    public Object approveAgency(@RequestBody DtsUserAccount account) {
        log.debug("设置佣金比例方法");
        log.debug("前端传的用户id为{}佣金比例为：{}", account.getUserId(), account.getSettlementRate());
        boolean flag = userService.approveAgency(account);
        if (flag) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail(-1, "数据更新失败");
        }
    }

    /**
     * 会员推广代理
     *
     * @return
     */
    @RequiresPermissions("admin:user:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "用户管理"}, button = "会员推广")
    @GetMapping("/detailApprove")
    public Object detailApprove(Integer id) {
        log.debug("会员推广代理方法开始执行");
        log.debug("前端传的推广代理数据的ID：{}", id);
//      前端要一个     DtsUserAccount  数据

//        List<DtsUserAccount>data=accountService.detailApprove(id);

        DtsUserAccount data=accountService.detailApprove(id);

        return ResponseUtil.ok(data);
//        return null;
    }
}
